import type { IBreadCrumb, ITagItem } from '@/types/store/app.js'
import { defineStore } from 'pinia'

const useAppStore = defineStore('app', {
  state: () => {
    return {
      language: (localStorage.getItem('lang') as 'zh-CN' | 'en-US' | null) || 'zh-CN', // 语言
      theme: 'Bright' as 'Bright' | 'Dark', // 主题
      collapse: false as boolean, // 折叠菜单
      tags: [] as ITagItem[], //  菜单标签
      breadCrumb: [] as IBreadCrumb[], // 面包屑
      machineType: 'desktop' as 'desktop' | 'mobile' | '',
    }
  },

  actions: {
    changeLanguage(lang: 'zh-CN' | 'en-US'): void {
      localStorage.setItem('lang', lang)
      this.language = lang
    },
    handleCollapse() {
      this.collapse = !this.collapse
    },
    closeCollapse() {
      this.collapse = false
    },
    openCollapse() {
      this.collapse = true
    },
    setMachineType(data: 'desktop' | 'mobile' | '') {
      this.machineType = data
    },
    addTags(tag: ITagItem) {
      if (!this.tags.some(val => val.path === tag.path)) {
        this.tags.push(tag)
      }
    },
    deleteTags(tag: ITagItem) {
      const index = this.tags.findIndex(val => val.path === tag.path)
      this.tags.splice(index, 1)
    },
    getSelectTagIndex(fullPath: string) {
      return this.tags.findIndex(val => val.path === fullPath) === -1
        ? 0
        : this.tags.findIndex(val => val.path === fullPath)
    },
    getMenuData() { },
    setBreadCrumb(breadCrumb: IBreadCrumb[]) {
      this.breadCrumb = breadCrumb
    },
  },
  getters: {
    // theme: (state) => state.theme,
    // language: (state) => state.language,
    // collapse: (state) => state.collapse,
    test: state => state.language,
  },
})
export default useAppStore
